package common.com.androidbase.datarequest;

import com.android.sohu.sdk.common.toolbox.DeviceUtils;
import com.android.sohu.sdk.common.toolbox.StringUtils;
import com.common.sdk.net.connect.http.DaylilyRequest;
import com.common.sdk.net.connect.http.util.SohuRequestBuilder;

import java.util.HashMap;
import java.util.Map;

import okhttp3.HttpUrl;
import okhttp3.Request;

/**
 * Created by sangchengjiang on 16/9/5.
 */
public class DataRequestUtil {


    private static final String FILE_SEPARATOR = "/";

    public static final String API_KEY = "9854b2afa779e1a6bff1962447a09dbd";


    public final static String FORMAL_DOMAIN_SOHU_TV = "http://s1.api.tv.itc.cn";

    private static final String GET_VIDEO_DETAIL = "/v4/video/info/%d.json";

    public static Request getVideoDetailRequest(long aid, long vid, int site) {
        String urlstr = combineRequestUrlWithParam(FORMAL_DOMAIN_SOHU_TV, GET_VIDEO_DETAIL, vid);

        // 是否为ugc(1:sohu_vrs, 2:sohu_ugc) 默认1
        if (site == 0) {
            site = 1;
        }


        // 测试SohuRequestBuilder
        Map<String, Object> params = new HashMap<>();
        params.put("site", site);
        if (IDTools.isNotEmpty(aid)) {
            params.put("aid", aid);
        }
        Request request = SohuRequestBuilder.buildGetRequest(urlstr, params);

        return request;
    }

    public static Request getExchangeInfo() {
        String url = "https://api.binance.com//api/v1/exchangeInfo";
        Request request = SohuRequestBuilder.buildGetRequest(url, null);
        return request;
    }

    public static DaylilyRequest getVideoDetail(long aid, long vid, int site) {
        if (IDTools.isEmpty(vid)) {
            return null;
        }


        String url = combineRequestUrlWithParam(FORMAL_DOMAIN_SOHU_TV, GET_VIDEO_DETAIL, vid);
        DaylilyRequest request = new DaylilyRequest(url, DaylilyRequest.Method.GET);
        addGetBaseParams(request);
        if (IDTools.isNotEmpty(aid)) {
            request.addQueryParam("aid", aid);
        }
        request.addQueryParam("site", site);
        return request;
    }

    public static String combineRequestUrlWithParam(String host, String method, long param) {
        String url = String.format(combineRequestUrl(host, method), param);
        return url;
    }

    public static String combineRequestUrl(String host, String method) {
        if (StringUtils.isNotBlank(host)) {
            if (host.endsWith(FILE_SEPARATOR)) {
                host = host.substring(0, host.length() - 1);
            }
        }
        if (StringUtils.isNotBlank(method)) {
            if (!method.startsWith(FILE_SEPARATOR)) {
                method = FILE_SEPARATOR + method;
            }
        }
        return (host + method);
    }

    /**
     * 请求的基础信息，必填<br>
     * 基础信息包括：api_key、plat、sver、poid、partner
     *
     * @param request
     */
    protected static void addGetBaseParams(DaylilyRequest request) {
        request.addQueryParam("api_key", API_KEY);
        request.addQueryParam("plat", "6");
        request.addQueryParam("sver", "5.9.0");
        request.addQueryParam("poid", "1");
        request.addQueryParam("partner", "680");
        request.addQueryParam("sysver", "5.1");
    }

    protected static void addGetBaseParams(HttpUrl.Builder builder) {
        builder.addQueryParameter("api_key", API_KEY);
        builder.addQueryParameter("plat", "6");
        builder.addQueryParameter("sver", "5.9.0");
        builder.addQueryParameter("poid", "1");
        builder.addQueryParameter("partner", "680");
        builder.addQueryParameter("sysver", "5.1");
    }

    public static Request getChannelV4PgcSubsData(String cursor) {
//        String url = combineRequestUrl("http://push.my.tv.sohu.com", "/v4/chase/pgc/subStream.json");

//        String url = combineRequestUrl("http://testapi.hd.sohu.com", "/v4/chase/pgc/subStream.json");

        String url = combineRequestUrl("http://devpush.my.tv.sohu.com", "/v4/chase/pgc/subStream.json");

        Map<String, Object> params = new HashMap<>();

        params.put("uid", "561e78867b81cba5f4527656a351252e");
        params.put("cursor", cursor);
        // 变更标识，不传表示5.2需求，3表示增加猜你喜欢
        params.put("flag", 3);

        Request okRequest = SohuRequestBuilder.buildGetRequest(url, params);
        return okRequest;
    }
}
